home *** CD-ROM | disk | FTP | other *** search
/ Clickx 96 / Clickx 96.iso / software / tools / tool / xbmc-10.1.exe / addons / script.module.pil / lib / PIL / IcoImagePlugin.py < prev    next >
Encoding:
Python Source  |  2009-04-06  |  1.9 KB  |  87 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id$
  4. #
  5. # Windows Icon support for PIL
  6. #
  7. # Notes:
  8. #       uses BmpImagePlugin.py to read the bitmap data.
  9. #
  10. # History:
  11. #       96-05-27 fl     Created
  12. #
  13. # Copyright (c) Secret Labs AB 1997.
  14. # Copyright (c) Fredrik Lundh 1996.
  15. #
  16. # See the README file for information on usage and redistribution.
  17. #
  18.  
  19.  
  20. __version__ = "0.1"
  21.  
  22. import Image, BmpImagePlugin
  23.  
  24.  
  25. #
  26. # --------------------------------------------------------------------
  27.  
  28. def i16(c):
  29.     return ord(c[0]) + (ord(c[1])<<8)
  30.  
  31. def i32(c):
  32.     return ord(c[0]) + (ord(c[1])<<8) + (ord(c[2])<<16) + (ord(c[3])<<24)
  33.  
  34.  
  35. def _accept(prefix):
  36.     return prefix[:4] == "\0\0\1\0"
  37.  
  38. ##
  39. # Image plugin for Windows Icon files.
  40.  
  41. class IcoImageFile(BmpImagePlugin.BmpImageFile):
  42.  
  43.     format = "ICO"
  44.     format_description = "Windows Icon"
  45.  
  46.     def _open(self):
  47.  
  48.         # check magic
  49.         s = self.fp.read(6)
  50.         if not _accept(s):
  51.             raise SyntaxError, "not an ICO file"
  52.  
  53.         # pick the largest icon in the file
  54.         m = ""
  55.         for i in range(i16(s[4:])):
  56.             s = self.fp.read(16)
  57.             if not m:
  58.                 m = s
  59.             elif ord(s[0]) > ord(m[0]) and ord(s[1]) > ord(m[1]):
  60.                 m = s
  61.             #print "width", ord(s[0])
  62.             #print "height", ord(s[1])
  63.             #print "colors", ord(s[2])
  64.             #print "reserved", ord(s[3])
  65.             #print "planes", i16(s[4:])
  66.             #print "bitcount", i16(s[6:])
  67.             #print "bytes", i32(s[8:])
  68.             #print "offset", i32(s[12:])
  69.  
  70.         # load as bitmap
  71.         self._bitmap(i32(m[12:]))
  72.  
  73.         # patch up the bitmap height
  74.         self.size = self.size[0], self.size[1]/2
  75.         d, e, o, a = self.tile[0]
  76.         self.tile[0] = d, (0,0)+self.size, o, a
  77.  
  78.         return
  79.  
  80.  
  81. #
  82. # --------------------------------------------------------------------
  83.  
  84. Image.register_open("ICO", IcoImageFile, _accept)
  85.  
  86. Image.register_extension("ICO", ".ico")
  87.